c# replace characters in string that are invalid using regex

95

c# replace characters in string that are invalid using regex -

string pattern = "[\\~#%&*{}/:<>?|\"-]";
string replacement = " ";

Regex regEx = new Regex(pattern);
string sanitized = Regex.Replace(regEx.Replace(input, replacement), @"\s+", " ");

This will replace runs of whitespace with a single space as well.

Comments

Submit
0 Comments